package goescape

import "fmt"

type (
	colors struct {
		// Foreground colors.
		Foreground,
		// Background colors.
		Background,
		// Underline colors.
		Underline color
	}

	color struct {
		// RGB color.
		Custom func(r, g, b uint8) Escape
		// Black color.
		Black,
		// Red color.
		Red,
		// Green color.
		Green,
		// Yellow color.
		Yellow,
		// Blue color.
		Blue,
		// Magenta color.
		Magenta,
		// Cyan color.
		Cyan,
		// White color.
		White,
		// Reset color.
		Reset Escape
	}
)

// Return escape code for color control.
func Color() colors {
	return colors{
		Foreground: color{
			Custom: func(r, g, b uint8) Escape {
				return Escape(fmt.Sprintf("\x1b[38;2;%d;%d;%dm", r, g, b))
			},
			Black:   "\x1b[38;2;0;0;0m",
			Red:     "\x1b[38;2;255;0;0m",
			Green:   "\x1b[38;2;0;255;0m",
			Yellow:  "\x1b[38;2;255;255;0m",
			Blue:    "\x1b[38;2;0;0;255m",
			Magenta: "\x1b[38;2;255;0;255m",
			Cyan:    "\x1b[38;2;85;255;255m",
			White:   "\x1b[38;2;255;255;255m",
			Reset:   "\x1b[39m",
		},
		Background: color{
			Custom: func(r, g, b uint8) Escape {
				return Escape(fmt.Sprintf("\x1b[48;2;%d;%d;%dm", r, g, b))
			},
			Black:   "\x1b[48;2;0;0;0m",
			Red:     "\x1b[48;2;128;0;0m",
			Green:   "\x1b[48;2;0;128;0m",
			Yellow:  "\x1b[48;2;128;128;0m",
			Blue:    "\x1b[48;2;0;0;128m",
			Magenta: "\x1b[48;2;128;0;128m",
			Cyan:    "\x1b[48;2;0;128;128m",
			White:   "\x1b[48;2;170;170;170m",
			Reset:   "\x1b[49m",
		},
		Underline: color{
			Custom: func(r, g, b uint8) Escape {
				return Escape(fmt.Sprintf("\x1b[58;2;%d;%d;%dm", r, b, b))
			},
			Black:   "\x1b[58;2;0;0;0m",
			Red:     "\x1b[58;2;255;0;0m",
			Green:   "\x1b[58;2;0;255;0m",
			Yellow:  "\x1b[58;2;255;255;0m",
			Blue:    "\x1b[58;2;0;0;255m",
			Magenta: "\x1b[58;2;255;0;255m",
			Cyan:    "\x1b[58;2;85;255;255m",
			White:   "\x1b[58;2;255;255;255m",
			Reset:   "\x1b[59m",
		},
	}
}
